HackerRank Stock Maximize
https://www.hackerrank.com/challenges/stockmax/problem
提出
11/12 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'stockmax' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER_ARRAY prices as parameter.
#
def stockmax(prices):
# Write your code here
money = 0
stock = 0
for i in range(len(prices)-1):
if (pricesi+1 > pricesi):
stock += 1
money -= pricesi
else:
money += stock * pricesi
stock = 0
return money + stock*prices-1
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
prices = list(map(int, input().rstrip().split()))
result = stockmax(prices)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'stockmax' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER_ARRAY prices as parameter.
#
def stockmax(prices):
# Write your code here
maxarr = [prices-1]
for i, p in enumerate(reversed(prices)):
if i == 0:
continue
else:
if p < maxarr-1:
maxarr.append(maxarr-1)
else:
maxarr.append(p)
maxarr = list(reversed(maxarr))
ans = 0
for i in range(len(prices)):
ans += maxarri - pricesi
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
prices = list(map(int, input().rstrip().split()))
result = stockmax(prices)
fptr.write(str(result) + '\n')
fptr.close()
メモ
Hackerrank - Stock Maximize Solution